package com.yunfei.api.v2.service;

import com.google.gson.Gson;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.contants.AppKeyContant;
import com.yunfei.api.v2.enums.CancelElecHtEnum;
import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.ContractIdParam;
import com.yunfei.api.v2.param.CustNoParam;
import com.yunfei.api.v2.param.ReserveNoParam;
import com.yunfei.api.v2.result.queryHeTongDetail.HeTongDetailResultJson;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.AutoJiaGaiZhangMapper;
import com.yunfei.mapper.aiiidb.HeTongSignInfoMapper;
import com.yunfei.utils.http.HttpIOUtil;
import com.yunfei.utils.http.QueryHeTongDetailHttpUtil;
import com.yunfei.utils.string.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class HTSGetDetailService {
    @Autowired
    private HeTongSignInfoMapper heTongSignInfoMapper;
    @Autowired
    private AutoJiaGaiZhangMapper autoJiaGaiZhangMapper;
    @Autowired
    private Environment env;
    /**
    * @Description: 根据合同id，查询合同详情
    * @Author: zhengyunfei
    * @Date: 2019/8/27
    * @time:11:03
    */
    public ResultModel getDetail(ContractIdParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        String appId=env.getProperty(AppKeyContant.appId);
        String appKey=env.getProperty(AppKeyContant.appKey);
        try{
            String contractId=param.getContractId();//合同id
            if(StringUtil.isNullOrEmpty(contractId)){
                resultModel.setCode(CancelElecHtEnum.CONTRACT_ID_NOTNULL.getCode());
                resultModel.setMessage(CancelElecHtEnum.CONTRACT_ID_NOTNULL.getMessage());
                return resultModel;
            }
            HttpIOUtil httpIOUtil=new HttpIOUtil();
            String token= httpIOUtil.getLongToken(appId,appKey,"");
            String url= YunHeTongApiContant.DOMAIN+"/contract/detail/0/"+contractId;
            JSONObject jsonObject= QueryHeTongDetailHttpUtil.get(url,token);
            Gson gson=new Gson();
            //HeTongDetailResultJson resultJson= (HeTongDetailResultJson) gson.fromJson(jsonObject.toString(),HeTongDetailResultJson.class);
            HeTongDetailResultJson resultJson= (HeTongDetailResultJson) JSONObject.toBean(jsonObject,HeTongDetailResultJson.class);
            resultModel.setData(resultJson);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
   /**
   * @Description: 根据预约编号获取合同id
   * @Author: zhengyunfei
   * @Date: 2019/8/27
   * @time:11:03
   */
    public ResultModel getContractId(ReserveNoParam param) {
        ResultModel resultModel=new ResultModel();
        String reserveNo=param.getReserveNo();
        if(StringUtil.isNullOrEmpty(reserveNo)){
            resultModel.setCode(GetAccountInfoEnum.RESERVENO_NOTNULL.getCode());
            resultModel.setMessage(GetAccountInfoEnum.RESERVENO_NOTNULL.getMessage());
            return resultModel;
        }
        Map<String,Object> queryMap=new HashMap<>();
        try{
            queryMap.put("reserveNo",reserveNo);
            String contractId= autoJiaGaiZhangMapper.getContractId(queryMap);
            if(!StringUtil.isNullOrEmpty(contractId)){
                resultModel.setData(contractId);
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            }else{
                resultModel.setData("");
                resultModel.setCode(ResultStatus.NO_DATA.getCode());
                resultModel.setMessage(ResultStatus.NO_DATA.getMessage());
            }

        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }

    public Object getSignerId(CustNoParam param) {
        ResultModel resultModel=new ResultModel();
        String custNo=param.getcCustNo();
        if(StringUtil.isNullOrEmpty(custNo)){
            resultModel.setCode(GetAccountInfoEnum.CUSTNO_NOTNULL.getCode());
            resultModel.setMessage(GetAccountInfoEnum.CUSTNO_NOTNULL.getMessage());
            return resultModel;
        }
        Map<String,Object> queryMap=new HashMap<>();
        try{
            queryMap.put("custNo",custNo);
            String signerId= autoJiaGaiZhangMapper.getSignerId(queryMap);
            if(!StringUtil.isNullOrEmpty(signerId)){
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                resultModel.setData(signerId);
            }else{
                resultModel.setData("");
                resultModel.setCode(ResultStatus.NO_DATA.getCode());
                resultModel.setMessage(ResultStatus.NO_DATA.getMessage());
            }


        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
}
